#include <sys/system.h>
#include <sys/task.h>
#include <ial/input.h>
#include <pages/pagectl.h>
#include <pages/page.h>

static pt_char_t *m_lpcszKeyNames[PT_INPUT_KEY_MAX] = {
		"?", "Left Mouse Button", "Right Mouse Button", "?", "Middle Mouse Button",
		"?", "?", "?", "Backspace", "Tab", "?", "?", "?", "Enter", "?", "?",
		"Shift", "Ctrl", "Alt", "Pause", "Caps Lock", "?", "?", "?", "?", "?", "?",
		"Escape", "?", "?", "?", "?",
		"Space", "Page Up", "Page Down", "End", "Home",
		"Left Arrow", "Up Arrow", "Right Arrow", "Down Arrow",
		"?", "?", "?", "?", "Insert", "Delete", "?",
		"0", "1", "2", "3", "4", "5", "6", "7", "8", "9",
		"?", "?", "?", "?", "?", "?", "?",
		"A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M",
		"N", "O", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z",
		"Left Win", "Right Win", "Application", "?", "?",
		"NumPad 0", "NumPad 1", "NumPad 2", "NumPad 3", "NumPad 4",
		"NumPad 5", "NumPad 6", "NumPad 7", "NumPad 8", "NumPad 9",
		"Multiply", "Add", "?", "Subtract", "Decimal", "Divide",
		"F1", "F2", "F3", "F4", "F5", "F6", "F7", "F8", "F9", "F10", "F11", "F12",
		"?", "?", "?", "?", "?", "?", "?", "?", "?", "?",
		"?", "?", "?", "?", "?", "?", "?", "?", "?", "?",
		"Num Lock", "Scroll Lock",
		"?", "?", "?", "?", "?", "?", "?", "?", "?", "?",
		"?", "?", "?", "?", "?", "?", "?", "?", "?", "?",
		"?", "?", "?", "?", "?", "?", "?", "?", "?", "?",
		"?", "?", "?", "?", "?", "?", "?", "?", "?", "?",
		"Semicolon", "Equals", "Comma", "Minus", "Period", "Slash", "Grave",
		"?", "?", "?", "?", "?", "?", "?", "?", "?", "?",
		"?", "?", "?", "?", "?", "?", "?", "?", "?", "?",
		"?", "?", "?", "?", "?", "?",
		"Left bracket", "Backslash", "Right bracket", "Apostrophe",
		"?", "?", "?", "?", "?", "?", "?", "?", "?", "?",
		"?", "?", "?", "?", "?", "?", "?", "?", "?", "?",
		"?", "?", "?", "?", "?", "?", "?", "?", "?", "?",
		"?", "?", "?"
	};
static pt_int16_t m_nKeyStates[PT_INPUT_KEY_MAX], m_nOldKeyStates[PT_INPUT_KEY_MAX];
static pt_bool_t m_bMouseOver;
static pt_uint16_t m_fPosX, m_fPosY;
static pt_uint16_t m_nPosZ;
static pt_uint16_t m_nVKLast;
static pt_char_t m_chLast;
static HWND m_hWnd;

void pt_input_system_init(HWND hWnd)
{
	POINT	point;

	pt_memset(m_nKeyStates, 0, sizeof(m_nKeyStates));
	pt_memset(m_nOldKeyStates, 0, sizeof(m_nOldKeyStates));

	m_bMouseOver = PT_FALSE;
	m_fPosX = 0;
	m_fPosY = 0;
	m_nPosZ = 0;
	
	m_nVKLast = 0;
	m_chLast = 0;

	m_hWnd = hWnd;
	GetCursorPos(&point);
	ScreenToClient(m_hWnd, &point);
	m_fPosX=(pt_uint16_t)point.x;
	m_fPosY=(pt_uint16_t)point.y;
}

pt_bool_t pt_input_system_update(void)
{
	int i;
	POINT	point;
	RECT	rect;

	m_nVKLast=0;
	m_chLast=0;
	pt_memcpy(m_nOldKeyStates, m_nKeyStates, sizeof(m_nKeyStates));
	for (i = 0; i < PT_INPUT_KEY_MAX; i++) {
		m_nKeyStates[i] = GetKeyState(i);
	}

	GetCursorPos(&point);
	GetClientRect(m_hWnd, &rect);
	MapWindowPoints(m_hWnd, NULL, (LPPOINT)&rect, 2);
	m_bMouseOver=PtInRect(&rect, point) && WindowFromPoint(point)==m_hWnd;
	ScreenToClient(m_hWnd, &point);
	m_fPosX=(pt_uint16_t)point.x;
	m_fPosY=(pt_uint16_t)point.y;

	for (i = 0; i < PT_INPUT_KEY_MAX; i++) {
		if (pt_input_is_key_down(i)) {
			pt_post_message(PT_TSK_ID_PAGE_CTRL, EV_PAGECTL_KEY_ON, 0, 0);
		}

		if (pt_input_is_key_up(i)) {
			pt_post_message(PT_TSK_ID_PAGE_CTRL, EV_PAGECTL_KEY_OFF, 0, 0);
		}
	}

	return PT_TRUE;
}

void pt_input_set_mouse_pos(pt_uint16_t x, pt_uint16_t y)
{
	POINT point;

	m_fPosX = x;
	m_fPosY = y;

	point.x = x;
	point.y = y ;

	ClientToScreen(m_hWnd, &point);
	SetCursorPos(point.x, point.y);
}

void pt_input_get_mouse_pos(pt_uint16_t *x, pt_uint16_t *y)
{
	*x = m_fPosX;
	*y = m_fPosY;
}

pt_uint16_t pt_input_get_mouse_wheel(void)
{
	return m_nPosZ;
}

pt_bool_t pt_input_is_mouse_over(pt_uint16_t nVKey)
{
	return m_bMouseOver;
}

pt_bool_t pt_input_is_key_down(pt_uint16_t nVKey)
{
	if (GetFocus()!=m_hWnd) return PT_FALSE;
	return m_nKeyStates[nVKey]<0 && m_nOldKeyStates[nVKey]>=0;
}

pt_bool_t pt_input_is_key_pressd(pt_uint16_t nVKey)
{
	if (GetFocus()!=m_hWnd) return PT_FALSE;
	return m_nKeyStates[nVKey]<0;
}

pt_bool_t pt_input_is_key_up(pt_uint16_t nVKey)
{
	if (GetFocus()!=m_hWnd) return PT_FALSE;
	return m_nKeyStates[nVKey]>=0 && m_nOldKeyStates[nVKey]<0;
}

pt_uint16_t pt_input_get_key(void)
{
	return m_nVKLast;
}

pt_char_t pt_input_get_char(void)
{
	return m_chLast;
}

pt_char_t *pt_input_get_key_name(pt_uint16_t nVKey)
{
	return m_lpcszKeyNames[nVKey];
}

